#Libraries

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.0      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(patchwork)

#Import Data

weather_df = 
  rnoaa::meteo_pull_monitors(
    c("USW00094728", "USC00519397", "USS0023B17S"),
    var = c("PRCP", "TMIN", "TMAX"), 
    date_min = "2017-01-01",
    date_max = "2017-12-31") %>%
  mutate(
    name = recode(
      id, 
      USW00094728 = "CentralPark_NY", 
      USC00519397 = "Waikiki_HA",
      USS0023B17S = "Waterhole_WA"),
    tmin = tmin / 10,
    tmax = tmax / 10) %>%
  select(name, id, everything())
## Registered S3 method overwritten by 'hoardr':
##   method           from
##   print.cache_info httr
## using cached file: ~/Library/Caches/R/noaa_ghcnd/USW00094728.dly
## date created (size, mb): 2022-09-29 10:33:58 (8.401)
## file min/max dates: 1869-01-01 / 2022-09-30
## using cached file: ~/Library/Caches/R/noaa_ghcnd/USC00519397.dly
## date created (size, mb): 2022-09-29 10:34:03 (1.699)
## file min/max dates: 1965-01-01 / 2020-03-31
## using cached file: ~/Library/Caches/R/noaa_ghcnd/USS0023B17S.dly
## date created (size, mb): 2022-09-29 10:34:06 (0.95)
## file min/max dates: 1999-09-01 / 2022-09-30

Create scatterplot with ggplot()

weather_df %>% 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5)
## Warning: Removed 15 rows containing missing values (geom_point).

Create labels and titles with labs()

weather_df %>% 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    caption = "Data from the rnoaa package"
  )
## Warning: Removed 15 rows containing missing values (geom_point).

Create scales

weather_df %>% 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    caption = "Data from the rnoaa package") + 
  scale_x_continuous(
    breaks = c(-15, 0, 15), 
    labels = c("-15º C", "0", "15"))
## Warning: Removed 15 rows containing missing values (geom_point).

weather_df %>% 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    caption = "Data from the rnoaa package") + 
  scale_x_continuous(
    breaks = c(-15, 0, 15), 
    labels = c("-15ºC", "0", "15"),
    limits = c(-20, 30)) + 
  scale_y_continuous(
    trans = "sqrt", 
    position = "right")
## Warning in self$trans$transform(x): NaNs produced
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 90 rows containing missing values (geom_point).

#Changing aesthetics wtith scale_color_hue()

weather_df %>% 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    caption = "Data from the rnoaa package") + 
  scale_color_hue(name = "Location", h = c(100, 300))
## Warning: Removed 15 rows containing missing values (geom_point).

Use Viridis package for color pallate; shows things as distinctly as they can be

ggp_temp_plot = 
  weather_df %>% 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    caption = "Data from the rnoaa package"
  ) + 
  viridis::scale_color_viridis(
    name = "Location", 
    discrete = TRUE
  )

ggp_temp_plot
## Warning: Removed 15 rows containing missing values (geom_point).

Themes

#moves legend to bottom:

ggp_temp_plot + 
  theme(legend.position = "bottom")
## Warning: Removed 15 rows containing missing values (geom_point).

legend.position = "none" will remove the legend.

Change to black and white

ggp_temp_plot + 
  theme_bw() + 
  theme(legend.position = "bottom")
## Warning: Removed 15 rows containing missing values (geom_point).

#Change to theme_classic

ggp_temp_plot + 
  theme_classic() + 
  theme(legend.position = "bottom")
## Warning: Removed 15 rows containing missing values (geom_point).

Another theme from ggthemes

ggp_temp_plot + 
  ggthemes::theme_excel() + 
  theme(legend.position = "bottom")
## Warning: Removed 15 rows containing missing values (geom_point).

Data in geom()

central_park = 
  weather_df %>% 
  filter(name == "CentralPark_NY")

waikiki = 
  weather_df %>% 
  filter(name == "Waikiki_HA")

ggplot(data = waikiki, aes(x = date, y = tmax, color = name)) + 
  geom_point() + 
  geom_line(data = central_park)
## Warning: Removed 3 rows containing missing values (geom_point).

Patchwork

tmax_tmin_p = 
  weather_df %>%
  ggplot(aes(x = tmin, y = tmax, color = name)) +
  geom_point()

prcp_dens_p = 
  weather_df %>%
  filter(prcp > 0) %>%
  ggplot(aes(x = prcp, fill = name)) +
  geom_density(alpha = .5) +
  theme(legend.position = "none")

seasonality_p = 
  weather_df %>%
  ggplot(aes(x = date, y = tmax, color = name))+
  geom_point(alpha = .5) +
  theme(legend.position = "none")

#Combine all together

tmax_tmin_p + prcp_dens_p / seasonality_p
## Warning: Removed 15 rows containing missing values (geom_point).
## Warning: Removed 3 rows containing missing values (geom_point).

#Data manipulation for visualization #see how factors work; put things in order from smallest to greatest based on median of tmax

weather_df %>%
  mutate(name = fct_reorder(name, tmax)) %>%
  ggplot(aes(x = name, y = tmax)) +
  geom_boxplot()
## Warning: Removed 3 rows containing non-finite values (stat_boxplot).

#Clean data then define aesthetic mappings I care about # create a plot showing BDI score across visits

pulse_df =
  haven::read_sas("data/public_pulse_data.sas7bdat") %>%
  janitor::clean_names() %>%
  pivot_longer(
    bdi_score_bl:bdi_score_12m,
    names_to = "visit",
    values_to = "bdi",
    names_prefix = "bdi_score_"
      ) %>%
  select(id, visit, everything())

pulse_df %>%
  ggplot(aes(x = visit, y = bdi)) +
  geom_boxplot()
## Warning: Removed 879 rows containing non-finite values (stat_boxplot).